function [train_ind,test_ind] = KFoldSplit(y,l_ind,K)

l_pos_ind = intersect(find(y==1)',l_ind);
l_neg_ind = intersect(find(y==-1)',l_ind);
l_pos_ind= l_pos_ind(randperm(numel(l_pos_ind)));
l_neg_ind= l_neg_ind(randperm(numel(l_neg_ind)));
l_pos_num = length(l_pos_ind);
l_neg_num = length(l_neg_ind);
t_pos_ind = reshape([l_pos_ind,zeros(1,ceil(l_pos_num/K)*K-l_pos_num)],K,[]);
t_neg_ind = reshape([zeros(1,ceil(l_neg_num/K)*K-l_neg_num),l_neg_ind],K,[]);
%{
pos and neg lebels will be divided uniformly to K splits
%}
test_ind = mat2cell([t_pos_ind,t_neg_ind],ones(1,K));
train_ind = cell(K,1);
for k = 1:K
    test_ind{k}(test_ind{k}==0) = [];
    train_ind{k} = setdiff(l_ind,test_ind{k});
end

end

